package com.cheyun.netsalev3.viewmodel

import android.content.Intent
import android.view.View
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import androidx.work.Data
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import com.cheyun.netsalev3.bean.Search
import com.cheyun.netsalev3.bean.dms.Product
import com.cheyun.netsalev3.repository.ChooseCarRepository
import com.cheyun.netsalev3.repository.SearchRepository
import com.cheyun.netsalev3.view.AdvancedSearchActivity
import com.cheyun.netsalev3.view.SearchResultActivity
import com.cheyun.netsalev3.workers.SearchDatabaseWorker
import com.jeremyliao.liveeventbus.LiveEventBus

/**
 * @author Michael
 * @date  2018/12/21 - 10:07.
 */
class SearchActivityViewModel(var nkey: String,var from:String, var searchRepository: SearchRepository) : BaseViewModel() {
    var keyword = ObservableField<String>("")
    fun search(view: View) {
        if (keyword.get().isNullOrBlank()) {

        } else {
            val myData = Data.Builder()
                    .putString("key", keyword.get().toString())
                    .build()
            val compressionWork = OneTimeWorkRequestBuilder<SearchDatabaseWorker>()
                    .setInputData(myData).build()
            WorkManager.getInstance().enqueue(compressionWork)
            startData(view, keyword.get().toString())
        }
    }

    /**
     * 开始搜索
     */
    fun startData(view: View, key: String) {
        var hashMap = HashMap<String, String>()
        hashMap["keyword"] = key
        if(from.equals("allocated")){
            LiveEventBus.get().with("allocatedsearch").postValue(hashMap)
            myFinish()
        }else{
            var intent = Intent(view.context, SearchResultActivity::class.java)
            intent.putExtra("nkey", nkey)
            intent.putExtra("hashMap", hashMap)
            view.context.startActivity(intent)
        }
    }

    /**
     * item点击
     */
//    fun searchItem(view: View, item: Search) {
//        startData(view, item.key)
//    }

    fun searchItem(pos:Int) {
        var item = searchlist.value?.get(pos)
//            startData(view, item.key)
    }

    fun searchSeries(view: View, item: Product) {
        var hashMap = HashMap<String, String>()
        hashMap["series"] = item.id.toString()
        if(from.equals("allocated")){
            LiveEventBus.get().with("allocatedsearch").postValue(hashMap)
            myFinish()
        }else{
            var intent = Intent(view.context, SearchResultActivity::class.java)
            intent.putExtra("nkey", nkey)
            intent.putExtra("hashMap", hashMap)
            view.context.startActivity(intent)
        }
    }

    fun advancedSearch(view: View) {
        //高级搜索
        var intent = Intent(view.context, AdvancedSearchActivity::class.java)
        intent.putExtra("nkey", nkey)
        view.context.startActivity(intent)
    }

    var searchlist = searchRepository.getlist()

    /**
     * d删除
     */
    fun deleteTabe(view: View) {
        searchRepository.deleteTabe()
    }

    fun onBack(view:View){
        myFinish()
    }

    init {
        ChooseCarRepository().getDMSseries("1","",this::setChex)
    }
    var carlist = MutableLiveData<List<Product>>()
    fun setChex(type:String,list: List<Product>) {
        carlist.postValue(list)
    }

}